import Data.List
import System.IO
import System.Environment




main = do    
{-
    let list3 = take3 [] ["4","+","3","+","5"]
    print list3

    
    let list3a = take3a [] ["1","+","13","+","5"]
    print list3a

    let list3b = take3b list3  list3a
    print "list3b="
    print list3b


    let (a, b) = take5 ([], []) ["4","+","3","*","5", "-", "7"]
    print a
    print b

-}
  
    let (a, b) = take4 ([], []) ["4","+","3","*","5", "+", "7"]
    print a
    print b
  
    print "******************:"




testfun11::[String]->([Char], [Char])
testfun11 a =("abcd", "1234" )  


testfun1::(Num a, Read a) => [String]->([a], [String])
testfun1 a  =( [12,3,4,5], ["+", "-", "*"]  )  


getOpPriority ::(Num a) => String-> a
getOpPriority x
    | x `elem` ["+","-"] = 1 
    | x `elem` [ "*", "/"] = 2
    

take4 ::(Num a, Read a) => ([a], [String])->[String]->([a], [String])
take4 (digits, operators) [] = (digits, operators)

{-
take4 (digits, []) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take4 (digits,  [x])  xs
    | otherwise =  take4 ( read x : digits, [])  xs

take4 ([],operators) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take4 ([],  (x:operators))  xs
    | otherwise =  take4 ( read x , operators)  xs

take4 ([],operators) (x:[])
    | x `elem` ["+","-", "*", "/"] = take4 ([],  (x:operators))  [x]
    | otherwise =  take4 ( read x , operators)  [x]

-}
take4 (digit1:digits, op1:operators) (x:x2:xs)
    | x `elem` ["+","-", "*", "/"] = 
        if getOpPriority x >  getOpPriority op1 
            then  take4 (  digit1 * read x2 : digits,  op1:operators)  (xs)
            else  take4 ( digit1:digits,  x:op1:operators)  (x2:xs)
    | otherwise =  take4 ( read x : digit1:digits, op1:operators)  (x2:xs)

take4 (digits, operators) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take4 (digits,  (x:operators))  xs
    | otherwise =  take4 ( read x : digits, operators)  xs

{-
take4 ::(Num a, Read a) => ([a], [String])->[String]->([a], [String])
take4 (digits, operators) [] = (digits, operators)

take4 (digits, []) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take4 (digits,  [x])  xs
    | otherwise =  take4 ( read x : digits, [])  xs

take4 ([],operators) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take4 ([],  (x:operators))  xs
    | otherwise =  take4 ( read x , operators)  xs

take4 ([],operators) (x:[])
    | x `elem` ["+","-", "*", "/"] = take4 ([],  (x:operators))  [x]
    | otherwise =  take4 ( read x , operators)  [x]

take4 (digit1:digits, op1:operators) (x:x2:xs)
    | x `elem` ["+","-", "*", "/"] = take4 ( digit1:digits,  x:op1:operators)  (x2:xs)    
    | otherwise =  take4 ( read x : digit1:digits, op1:operators)  (x2:xs)
-}


take5 ::(Num a, Read a) => ([a], [String])->[String]->([a], [String])
take5 (digits, operators) [] = (digits, operators)
take5 (digits, operators) (x:xs)
    | x `elem` ["+","-", "*", "/"] = take5 (digits,  (x:operators))  xs
    | otherwise =  take5 ( read x : digits, operators)  xs

take3 ::(Num a, Read a) => [String] -> [String] -> [a ]
take3 _ [] = []
take3 n (x:xs)
    | x `elem` ["+","-", "*", "/"] = take3 (x:n) xs
    | otherwise = read x : take3 n xs    
    
take3a ::[String] -> [String] -> [String ]
take3a _ [] = []
take3a n (x:xs)
    | x `elem` ["+","-", "*", "/"] = x : take3a n xs
    | otherwise = take3a n xs    
    
    
take3b ::(Num a) => [a] -> [String] -> [a]
take3b [x] [] = [x]
take3b [] _ = []
take3b (x:y:ys) ("+":xs) = take3b  (x + y:ys)  xs
take3b (x:y:ys) ("-":xs) = take3b  (x - y:ys)  xs
--take3b x n = x

